package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.*;
import com.sky.exception.BaseException;
import com.sky.mapper.*;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageList(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        Page<DishVO> dishPage = (Page<DishVO>) dishMapper.pageList(dishPageQueryDTO);

        List<DishVO> result = dishPage.getResult();
        for (DishVO dishVO : result) {
            CategoryDTO dto = new CategoryDTO();
            dto.setId(dishVO.getCategoryId());
            Category category = categoryMapper.findByCondition(dto);

            dishVO.setCategoryName(category.getName());
        }
        return new PageResult(dishPage.getTotal(), dishPage.getResult());
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public DishVO findById(Long id) {
        Dish dish = dishMapper.findById(id);
        List<DishFlavor> flavors = dishFlavorMapper.findByDishId(id);

        CategoryDTO dto = new CategoryDTO();
        dto.setId(dish.getCategoryId());
        Category category = categoryMapper.findByCondition(dto);

        return DishVO.builder()
                .categoryId(dish.getCategoryId())
                .categoryName(category.getName())
                .description(dish.getDescription())
                .flavors(flavors)
                .id(dish.getId())
                .image(dish.getImage())
                .name(dish.getName())
                .price(dish.getPrice())
                .status(dish.getStatus())
                .updateTime(dish.getUpdateTime())
                .build();
    }

    /**
     * 菜品起售、停售
     *
     * @param dishDTO
     * @param status
     */
    @Override
    public void updateStatus(DishDTO dishDTO, Integer status) {
        Dish dish = Dish.builder()
                .id(dishDTO.getId())
                .status(status)
                .build();

        SetmealDish setmealDish = setmealDishMapper.findByDishId(dish.getId());
        Setmeal setmeal = setmealMapper.findById(setmealDish.getSetmealId());

        if (setmealDish != null && setmeal.getStatus().equals(StatusConstant.ENABLE)) {
            throw new BaseException("当前菜品关联了正在起售的套餐，不能停售");
        }

        dishMapper.updateStatus(dish);
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<DishVO> findByCategoryId(Long categoryId) {
        DishPageQueryDTO dto = new DishPageQueryDTO();
        dto.setCategoryId(categoryId);

        return dishMapper.pageList(dto);
    }

    /**
     * 新增菜品
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void add(DishDTO dishDTO) {
        DishPageQueryDTO dto = new DishPageQueryDTO();
        dto.setName(dishDTO.getName());

        List<DishVO> dishes = dishMapper.pageList(dto);
        if (dishes != null) {
            throw new BaseException("菜品已存在，请重新输入菜品名称");
        }

        Dish dish = Dish.builder()
                .name(dishDTO.getName())
                .categoryId(dishDTO.getCategoryId())
                .price(dishDTO.getPrice())
                .image(dishDTO.getImage())
                .description(dishDTO.getDescription())
                .status(StatusConstant.ENABLE)
                .build();

        dishMapper.add(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors.size() != 0)
            for (DishFlavor flavor : flavors) {
                dishFlavorMapper.add(flavor);
            }
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void updateInfo(DishDTO dishDTO) {
        DishPageQueryDTO dto = new DishPageQueryDTO();
        dto.setName(dishDTO.getName());

        List<DishVO> dishes = dishMapper.pageList(dto);
        if (dishes != null && !dishes.get(0).getId().equals(dishDTO.getId())) {
            throw new BaseException("菜品已存在，请重新输入菜品名称");
        }

        Dish dish = Dish.builder()
                .id(dishDTO.getId())
                .name(dishDTO.getName())
                .categoryId(dishDTO.getCategoryId())
                .price(dishDTO.getPrice())
                .image(dishDTO.getImage())
                .description(dishDTO.getDescription())
                .status(StatusConstant.ENABLE)
                .build();

        dishMapper.updateInfo(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors.size() != 0) {
            dishFlavorMapper.delete(dishDTO.getId());

            for (DishFlavor flavor : flavors) {
                dishFlavorMapper.add(flavor);
            }
        }
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     */
    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            Dish dish = dishMapper.findById(id);
            if (dish.getStatus().equals(StatusConstant.ENABLE)) {
                throw new BaseException(MessageConstant.DISH_ON_SALE);
            }

            SetmealDish setmealDish = setmealDishMapper.findByDishId(id);
            if (setmealDish != null) {
                throw new BaseException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }

        dishMapper.delete(ids);
    }
}
